using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Microsoft.Ink;

namespace edu.stanford.hci.ideas
{
    class Timestamp
    {
        private static System.Guid TIMESTAMPGuid = new Guid("8A54CF58-97E6-4fc5-8F06-F8BAD2E19B22");

        public static long GetTimestampForStroke(Stroke s)
        {
            if (!s.ExtendedProperties.DoesPropertyExist(TIMESTAMPGuid))
            {
                // error: this stroke doesn't have a timestamp! but all strokes should have timestamps...
                return -1L;
            }
            ExtendedProperty ep = s.ExtendedProperties[TIMESTAMPGuid];
            Byte[] bt = (Byte[])ep.Data;
            BinaryReader binread = new BinaryReader(new MemoryStream(bt));
            long filetime = binread.ReadInt64();
            return filetime;
        }

        public static void SetTimestampForStroke(Stroke s)
        {
            try
            {
                //Retrieve the current time, UTC format.
                DateTime dt = DateTime.UtcNow;
                long filetime = dt.ToFileTime();

                //Write the time to a temporary byte array.
                MemoryStream memstr = new MemoryStream();
                BinaryWriter binwrite = new BinaryWriter(memstr);

                binwrite.Write(filetime);
                Byte[] bt = memstr.ToArray();

                memstr.Close();
                binwrite.Close();

                //Save the extended property on the stroke.
                s.ExtendedProperties.Add(TIMESTAMPGuid, bt);
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }
    }
}
